<template>
	<view>
		<list-filter :tabs="tabs" :curIndex="curTabIndex" style="position: fixed;left: 0;top: 0;z-index: 99999;width: 100%;"  @onChange="changeTab"></list-filter>
		<view style="margin-top: 55px;"></view>
		<list-mall  :num='geusts.num' :banner='geusts.banner' :datas='geusts.datas'></list-mall>
	</view>
</template>

<script>
	import listMall from '@/components/devm/index/list-mall.vue'
	import listFilter from '@/components/devm/list-filter.vue'
	
	var current = 1;
	export default {
		components:{
			listMall,listFilter
		},data(){
			return {
				geusts : {
					//商品信息				
					datas: [],
					//一行显示的商品个数	
					num: 2
				},
				tabs:[],
				curTabIndex:0,
				catas:[],
				cataid:'',//二级分类ID
			}
		},onLoad(options) {
			this.initSecondCata(options.cataid);
		},
		onReachBottom() {//滑动到最后一页，加载下一页
			current++;
			this.getproduct();
		},
		methods:{
			initSecondCata(refcataid) {
				var that = this;
				this.$get('mall/cata/commonlist',{type:'second',cataid:refcataid}).then((res) => {
					//console.log(res)
					if (res.data) {
						that.catas = res.data;
						for (var k = 0; k < res.data.length; k++) {
							that.tabs.push(res.data[k].name)
							if(k==0) {
								that.cataid=res.data[k].id;
								that.getproduct();//默认请求第一个分类产品信息
							}
						}
					}
				})
			},changeTab(index) {//分类切换
				if(this.curTabIndex == index) {
					return;
				}
				current = 1;//当前分类从第一页开始加载
				this.curTabIndex = index;
				this.cataid = this.catas[index].id;//切换分类ID，然后请求该分类
				this.geusts.datas = [];//先清空之前的列表
				this.getproduct();
			},
			getproduct() {
				var that = this;
				this.$get('mall/sku/commonpage?isShumbInfo=true',{current:1,cataid:this.cataid},'get',true).then((res) => {
					//console.log(res)
					if (res.data) {
						for (var k = 0; k < res.data.items.length; k++) {
							var item = {
								id: res.data.items[k].id,
								uri: res.data.items[k].thumbnail,
								title: res.data.items[k].name,
								specialprice: '￥' + res.data.items[k].priceSale,
								originprice: res.data.items[k].priceDel?'￥'+ res.data.items[k].priceDel:''
							}
							that.geusts.datas.push(item)
						}
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #F5F5F5;
	}
</style>
